<template>
  <el-dialog
    v-if="dialogVisible"
    :title="`${formData.id ? '编辑' : '新增'}文章`"
    :visible.sync="dialogVisible"
    @close="close"
  >
    <el-form
      ref="form"
      :model="formData"
      :rules="formRules"
      label-width="100px"
    >
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="formData.title" placeholder="请输入文章标题" />
      </el-form-item>
      <el-form-item label="文章内容" prop="articleBody">
        <Quill :context="formData.articleBody" @update="getContent" />
      </el-form-item>
      <el-form-item label="视频地址">
        <el-input v-model="formData.videoURL" />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="btnOk">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Quill from '@/components/Quill'
import { addArticle, getArticle, editArticle } from '@/api/articles'
export default {
  components: {
    Quill
  },
  data() {
    return {
      dialogVisible: false,
      formData: {},
      formRules: {
        title: [{ required: true, trigger: 'blur', message: '不能为空' }],
        articleBody: [{ required: true, trigger: 'blur', message: '不能为空' }]
      }
    }
  },
  methods: {
    close() {
      this.$refs.form.clearValidate()
      this.formData = {}
    },
    // 获取富文本框的值
    getContent(val) {
      this.formData.articleBody = val
    },
    async btnOk() {
      if (this.formData.id) {
        await editArticle(this.formData)
      } else {
        await addArticle(this.formData)
      }
      this.$message.success('操作成功')
      this.$emit('update')
      this.dialogVisible = false
    },
    async getArticle(id) {
      const res = await getArticle(id)
      this.formData = res
    }
  }
}
</script>

<style>
</style>
